package net.sppan.base.dao;

import net.sppan.base.entity.Employee;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface IEmployeeDao  {
    @Select(value = "SELECT\n" +
            "\te.id,\n" +
            "\te.employee_code as employeeCode,\n" +
            "\te.employee_sex as employeeSex,\n" +
            "\te.employee_name as employeeName,\n" +
            "\te.employee_phone employeePhone,\n" +
            "\te.employee_idcard  employeeIdcard,\n" +
            "\te.department_id departmentId,\n" +
            "\te.address,\n" +
            "\td.department_name as departmentName\n" +
            "FROM\n" +
            "\ttb_employee e left join tb_department d on e.department_id = d.id ")
    List<Employee> getEmployeeList(String employeeName);


    @Insert("INSERT INTO `base`.`tb_employee` ( `employee_code`, `employee_sex`, `employee_name`, `employee_phone`, `employee_idcard`, `department_id`, `address` )\n" +
            "VALUES " +
            "( #{employeeCode}, #{employeeSex}, #{employeeName}, #{employeePhone}, #{employeeIdcard}, #{departmentId}, #{address})")
    int saveEmployee(Employee employee);

    @Select("SELECT \n" +
            "            e.id, \n" +
            "            e.employee_code as employeeCode, \n" +
            "            e.employee_sex as employeeSex, \n" +
            "            e.employee_name as employeeName, \n" +
            "            e.employee_phone employeePhone, \n" +
            "            e.employee_idcard  employeeIdcard, \n" +
            "            e.department_id departmentId, \n" +
            "            e.address, \n" +
            "            d.department_name as departmentName \n" +
            "            FROM \n" +
            "            tb_employee e left join tb_department d on e.department_id = d.id where e.id = #{employeeId} limit 1")
    Employee getEmployee(Integer employeeId);

    @Update("UPDATE tb_employee \n" +
            "SET employee_name = #{employeeName},\n" +
            "employee_sex =${employeeSex},\n" +
            "employee_phone = #{employeePhone},\n" +
            "employee_idcard = #{employeeIdcard},\n" +
            "address= #{address},\n" +
            "department_id = #{departmentId} \n" +
            "WHERE\n" +
            "\t id = #{id} \n" +
            "\tAND employee_code = #{employeeCode}")
    int updateEmployee(Employee employee );


    @Delete(" delete from tb_employee where id = #{id}")
    int deleteEmployee(Integer id );
}
